﻿using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FaceRecognition
{
    public class FaceImpl : IFace
    {
        private CascadeClassifier faceClassifier;
        private CascadeClassifier eyeClassifier;
        public string log;
        public void LoadFaceRecognitionFile(string file)
        {
            
            try
            {
                faceClassifier = new CascadeClassifier(file);
                log = "识别文件载入成功";
            }
            catch(Exception ex)
            {
                MessageBox.Show("识别文件载入失败,详细原因\n"+ex.Message);
            }
        }
        public void LoadEyeRecognitionFile(string file)
        {
            try
            {
                eyeClassifier = new CascadeClassifier(file);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("识别文件载入失败,详细原因\n" + ex.Message+"\n"+ex.StackTrace);
            }
        }
        public List<Image<Bgr,byte>> GetFaceImgList( Image<Bgr,byte> img)
        {
            List<Image<Bgr, byte>> facelist = new List<Image<Bgr, byte>>();
            Rectangle[] faces = faceClassifier.DetectMultiScale(img,1.3, 3, new Size(40, 40));
            try
            {
                log = "检测中";
                foreach (Rectangle face in faces)
                {
                    CvInvoke.Rectangle(img, face, new Bgr(Color.Red).MCvScalar, 2);
                    CvInvoke.cvSetImageROI(img, face);
                    Image<Bgr, byte> roi = new Image<Bgr, byte>(face.Size);
                    CvInvoke.cvCopy(img, roi, IntPtr.Zero);
                    facelist.Add(roi);
                }
                if (facelist.Count != 0)
                    return facelist;
                else 
                {
                    facelist.Add(img);
                    return facelist;
                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show("脸部检测失败,详细原因\n" + ex.Message + "\n" + ex.StackTrace);
                facelist.Add(img);
                return facelist;
            }
        }

        public List<Image<Bgr, byte>> GetEyeImgList(Image<Bgr, byte> faceimg)
        {
            List<Image<Bgr, byte>> eyelist = new List<Image<Bgr, byte>>();
            Rectangle[] eyes = eyeClassifier.DetectMultiScale(faceimg, 1.3, 3, new Size(20, 20));
            try
            {
                log = "检测中";
                foreach (Rectangle eye in eyes)
                {
                    CvInvoke.Rectangle(faceimg, eye, new Bgr(Color.Green).MCvScalar, 2);
                    CvInvoke.cvSetImageROI(faceimg, eye);
                    Image<Bgr, byte> roi = new Image<Bgr, byte>(eye.Size);
                    CvInvoke.cvCopy(faceimg, roi, IntPtr.Zero);
                    eyelist.Add(roi);
                }
                if (eyelist.Count == 0)
                {
                    eyelist.Add(faceimg);
                    eyelist.Add(faceimg);
                    return eyelist;
                }
                else if (eyelist.Count == 1)
                {
                    eyelist.Add(faceimg);
                    return eyelist;
                }
                else
                    return eyelist;
              
            }
            catch (Exception ex)
            {
                MessageBox.Show("眼部检测检测失败,详细原因\n" + ex.Message + "\n" + ex.StackTrace);
                eyelist.Add(faceimg);
                return eyelist;
            }
        }
     
    }
}
